package com.hy.www;

import com.hy.www.db.MYSQLControl;
import com.hy.www.entity.JdEntity;
import com.hy.www.util.URLFecter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;


import java.util.List;

/**
 * @author hy
 * @date 20180809
 */
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class JavaPachongApplication {

    /** 日志 */
    static final Log logger = LogFactory.getLog(JavaPachongApplication.class);

    public static void main(String[] args) throws Exception {
        SpringApplication.run(JavaPachongApplication.class, args);
        HttpClient client = new DefaultHttpClient();
        //我们要爬取的一个地址，这里可以从数据库中抽取数据，然后利用循环，可以爬取一个URL队列
        String url="http://search.jd.com/Search?keyword=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0&enc=utf-8&spm=2.1.4";
        //抓取的数据
        List<JdEntity> bookdatas=URLFecter.URLParser(client, url);
        //循环输出抓取的数据
        for (JdEntity jd:bookdatas) {
            logger.info("bookID:"+jd.getBookID()+"\t"+"bookPrice:"+jd.getBookPrice()+"\t"+"bookName:"+jd.getBookName()+"\t"+"author:"+jd.getAuthor()+"\t"+"publishing:"+jd.getPublishing());
        }
        //将抓取的数据插入数据库
        MYSQLControl.executeInsert(bookdatas);
    }
}
